//
//  MyUnfinTaskViewController.m
//  EcmDoctor
//
//  Created by Mac on 16/2/26.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "MyUnfinTaskViewController.h"
#import "MyUnfinTaskTableViewCell.h"
#import "Onlquestion.h"
#import "AppUtils.h"
#import "UIImageView+AFNetworking.h"
#import "MJRefresh.h"
#import "QuestionOnlineDetailController.h"
#import "QuestionTelDetailController.h"
#import "ClinicDetailViewController.h"
#import "QuestionDetailController.h"

@interface MyUnfinTaskViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    __weak IBOutlet NSLayoutConstraint *btn1Width;
    
    __weak IBOutlet UITableView *_unfinTaskTv;
    __weak IBOutlet NSLayoutConstraint *btn3Width;
    NSMutableArray *_dataAry;
    NSArray *_keySrAry;
    NSArray *_cureTypeAry;
    UILabel *_tishil;
    NSArray *_tishiAry;
    NSString *_keyStr;
    UIImageView *_swImageView;
}
@end

@implementation MyUnfinTaskViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
  self.view.backgroundColor = [AppUtils getBackground];
    self.automaticallyAdjustsScrollViewInsets = NO;
    if(!self.finish)
     self.navigationItem.titleView=[AppUtils generateNavigationItemTitleViewWithTitle:@"我的未完成任务"];
    else  self.navigationItem.titleView=[AppUtils generateNavigationItemTitleViewWithTitle:@"我的已完成任务"];
    _cureTypeAry=@[@"免费义诊",@"在线问诊",@"电话问诊",@"门诊加号"];
    _tishiAry=@[@"暂时没有在线问诊信息，点击刷新",@"暂时没有电话问诊信息，点击刷新",@"暂时没有门诊信息，点击刷新"];
    _keyStr=@"";
    _unfinTaskTv.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       [self reloadtableViewWith:_keyStr];
        
    }];
    _swImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, 40)];
    _swImageView.image=[UIImage imageNamed:@"clinic_tabbg1"];
    [self.view addSubview:_swImageView];
    [self.view sendSubviewToBack:_swImageView];
    _keySrAry=@[@"",@"getOnPhoneInfo",@"getAllAddTimeAskInfo"];
    btn1Width.constant=kScreenWidth/3;
    btn3Width.constant=kScreenWidth/3;
  
    _unfinTaskTv.separatorStyle=UITableViewCellSeparatorStyleNone;
    UIImage *tableIcon = [UIImage imageNamed:@"btn_back"];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithImage:tableIcon style:UIBarButtonItemStylePlain target:self action:@selector(changeToNavRootController:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithPatternImage:tableIcon];
    [self reloadtableViewWith:nil];
}

-(void)reloadtableViewWith:(NSString *)keystr
{
    NSString *docId= [SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
  
    if(!keystr.length)
    {
        [Onlquestion getTaskNoFinishInfoById:docId success:^(NSArray *noTaskAry) {
            [_dataAry removeAllObjects];
            _dataAry=[NSMutableArray arrayWithArray:noTaskAry];
        [_unfinTaskTv.header endRefreshing];
            [_unfinTaskTv reloadData];
            
        } failure:^(NSError *error) {
         
        }];
    }
    else
    {
        [Onlquestion getTaskNoFinishInfoById:docId Keystr:keystr Isfinish:@"0" success:^(NSArray *noTaskAry) {
            [_dataAry removeAllObjects];
            _dataAry=[NSMutableArray arrayWithArray:noTaskAry];
           
            [_unfinTaskTv reloadData];
        } failure:^(NSError *error) {
               NSLog(@"%@",error.localizedDescription);
        }];
    }
  }

-(void)changeToNavRootController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!self.finish)
         return 120;
    else return 140;
}
static int qh=335;
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(!_dataAry.count)
    {
        if(_tishil==nil)
        {
            _tishil=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, 30)];
            
            _tishil.textAlignment=NSTextAlignmentCenter;
            _tishil.backgroundColor=[UIColor clearColor];
            
            _tishil.textColor=[UIColor blackColor];
            _tishil.font=[UIFont systemFontOfSize:15];
            
        }
        _tishil.text=_tishiAry[qh-335];
        [tableView addSubview:_tishil];
        
    }
    else
    {
        [_tishil removeFromSuperview];
    }

    return _dataAry.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell;
    MyUnfinTaskTableViewCell * myUnfinTaskCell;
    if(!self.finish)
    {
    myUnfinTaskCell=[tableView dequeueReusableCellWithIdentifier:@"myunfinishedtask" forIndexPath:indexPath];
       if(indexPath.row%2)
       myUnfinTaskCell.backView.backgroundColor=[UIColor whiteColor];
    else
         myUnfinTaskCell.backView.backgroundColor=[UIColor colorWithRed:250/255.0 green:243/255.0 blue:237/255.0 alpha:1];
        
    }
    else {
        myUnfinTaskCell=[tableView dequeueReusableCellWithIdentifier:@"unfinishedtask" forIndexPath:indexPath];
    }
    
    Onlquestion *onli=_dataAry[indexPath.row];
    myUnfinTaskCell.customerNamel.text=onli.name;
    myUnfinTaskCell.selectionStyle=UITableViewCellSelectionStyleNone;
    myUnfinTaskCell.customerAgel.text=[NSString stringWithFormat:@"%@",onli.age];
    myUnfinTaskCell.customerSexl.text=onli.sex;
    NSLog(@"---------%@",onli.info_type);
    myUnfinTaskCell.customerQutypel.text=_cureTypeAry[onli.info_type.intValue];
    myUnfinTaskCell.customerPayl.text=[NSString stringWithFormat:@"￥%@",onli.info_money];
    if(!onli.info_money)
    myUnfinTaskCell.customerPayl.text=@"￥免费";
    myUnfinTaskCell.customerQuinfol.text=onli.content;
    [myUnfinTaskCell.iconImageView  setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ecm-user.oss-cn-beijing.aliyuncs.com/image/%@",onli.icon_Url]] placeholderImage:[UIImage imageNamed:@"icon"]];
    cell=myUnfinTaskCell;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    Onlquestion *onli=_dataAry[indexPath.row];
    
    if ([onli.info_type intValue] == 0) {
        if (onli.picUrlAry.count != 0) {
           [self performSegueWithIdentifier:@"toYiZhen" sender:@{@"name":onli.name, @"sex":onli.sex, @"content":onli.content, @"voice":onli.voiceUrl, @"age":onli.age, @"picUrl":onli.picUrlAry, @"headImage":onli.icon_Url, @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":onli.ask_info_id}];
        } else {
            [self performSegueWithIdentifier:@"toYiZhen" sender:@{@"name":onli.name, @"sex":onli.sex, @"content":onli.content, @"voice":onli.voiceUrl, @"age":onli.age, @"headImage":onli.icon_Url, @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":onli.ask_info_id}];
        }
            
    } else if ([onli.info_type intValue] == 1) {
        if (onli.picUrlAry.count != 0) {
            [self performSegueWithIdentifier:@"toOnline" sender:@{@"name":onli.name, @"sex":onli.sex, @"content":onli.content, @"voice":onli.voiceUrl, @"age":onli.age, @"picUrl":onli.picUrlAry, @"headImage":onli.icon_Url, @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":onli.ask_info_id}];
        } else {
            [self performSegueWithIdentifier:@"toOnline" sender:@{@"name":onli.name, @"sex":onli.sex, @"content":onli.content, @"voice":onli.voiceUrl, @"age":onli.age, @"headImage":onli.icon_Url, @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":onli.ask_info_id}];
        }
        
    } else if ([onli.info_type intValue] == 2) {
        if (onli.picUrlAry.count != 0) {
            
            [self performSegueWithIdentifier:@"toTel" sender:@{@"name":onli.name, @"sex":onli.sex, @"content":onli.content, @"voice":onli.voiceUrl, @"age":onli.age, @"picUrl":onli.picUrlAry, @"headImage":onli.icon_Url, @"info":[NSString stringWithFormat:@"患者已经预约了 %@ %@ %@的电话咨询", onli.info_order_date, [AppUtils weekForClinic:onli.info_order_week], onli.info_order_time], @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":onli.ask_info_id}];
        } else {
            [self performSegueWithIdentifier:@"toTel" sender:@{@"name":onli.name, @"sex":onli.sex, @"content":onli.content, @"voice":onli.voiceUrl, @"age":onli.age, @"headImage":onli.icon_Url, @"info":[NSString stringWithFormat:@"患者已经预约了 %@ %@ %@的门诊加号", onli.info_order_date, [AppUtils weekForClinic:onli.info_order_week], onli.info_order_time], @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":onli.ask_info_id}];
        }
    } else
        if (onli.picUrlAry.count != 0) {
            
            [self performSegueWithIdentifier:@"toClinic" sender:@{@"name":onli.name, @"sex":onli.sex, @"content":onli.content, @"voice":onli.voiceUrl, @"age":onli.age, @"picUrl":onli.picUrlAry, @"headImage":onli.icon_Url, @"info":[NSString stringWithFormat:@"患者已经预约了 %@ %@ %@的门诊加号", onli.info_order_date, [AppUtils weekForClinic:onli.info_order_week], onli.info_order_time], @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":onli.ask_info_id}];
        } else {
            [self performSegueWithIdentifier:@"toClinic" sender:@{@"name":onli.name, @"sex":onli.sex, @"content":onli.content, @"voice":onli.voiceUrl, @"age":onli.age, @"headImage":onli.icon_Url, @"info":[NSString stringWithFormat:@"患者已经预约了 %@ %@ %@的门诊加号", onli.info_order_date, [AppUtils weekForClinic:onli.info_order_week], onli.info_order_time], @"doctorId":[SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY], @"askId":onli.ask_info_id}];
        }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}

- (IBAction)topSwitch:(UIButton *)sender {
    if(sender.tag!=qh)
    {
        UIButton *bfbtn=(UIButton*)[self.view viewWithTag:qh];
        
        [bfbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _swImageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"clinic_tabbg%d",(int)sender.tag-334]];
        //bfbtn.backgroundColor=[UIColor colorWithRed:123/255.0 green:103/255.0 blue:94/255.0 alpha:1];
        [sender setTitleColor:[UIColor colorWithRed:255/255.0 green:204/255.0 blue:102/255.0 alpha:1] forState:UIControlStateNormal];
        //sender.backgroundColor=[UIColor colorWithRed:42/255.0 green:18/255.0 blue:6/255.0 alpha:1];
        qh=(int)sender.tag;
        _keyStr=_keySrAry[(int)sender.tag-335];
       [self reloadtableViewWith:_keySrAry[(int)sender.tag-335]];
       
    }
}

/**/
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString:@"toOnline"]) {
        
        QuestionOnlineDetailController *detail = [segue destinationViewController];
        // 对详情页面赋值
        detail.voiceUrl = sender[@"voice"];
        detail.detail = sender[@"content"];
        detail.name = sender[@"name"];
        detail.sex = sender[@"sex"];
        detail.age = [NSString stringWithFormat:@"%@", sender[@"age"]];
        detail.imageUrl = sender[@"picUrl"];
        detail.headImageUrl = sender[@"headImage"];
        detail.doctorId = sender[@"doctorId"];
        detail.askId = sender[@"askId"];
        
        if (_finish == YES) {// _finish区分已完成任务和未完成任务
            detail.ifDownload = 3;
        } else
            detail.ifDownload = 2;
        
    } else if ([segue.identifier isEqualToString:@"toTel"]) {
        QuestionTelDetailController *detail = [segue destinationViewController];
        // 对详情页面赋值
        detail.voiceUrl = sender[@"voice"];
        detail.detail = sender[@"content"];
        detail.name = sender[@"name"];
        detail.sex = sender[@"sex"];
        detail.age = [NSString stringWithFormat:@"%@", sender[@"age"]];
        detail.imageUrl = sender[@"picUrl"];
        detail.headImageUrl = sender[@"headImage"];
        detail.infoStr = sender[@"info"];
        detail.doctorId = sender[@"doctorId"];
        detail.askId = sender[@"askId"];
        if (_finish == YES) {
            detail.ifDownload = 3;
        } else
            detail.ifDownload = 2;
        
    } else if ([segue.identifier isEqualToString:@"toClinic"]) {
        ClinicDetailViewController *clinic = [segue destinationViewController];
        clinic.detail = sender[@"content"];
        clinic.name = sender[@"name"];
        clinic.sex = sender[@"sex"];
        clinic.age = [NSString stringWithFormat:@"%@", sender[@"age"]];
        clinic.imageUrl = sender[@"picUrl"];
        clinic.headImageUrl = sender[@"headImage"];
        clinic.infoStr = sender[@"info"];
        clinic.doctorId = sender[@"doctorId"];
        clinic.askId = sender[@"askId"];
        if (_finish == YES) {
            clinic.ifDownload = 3;
        } else
            clinic.ifDownload = 2;
    } else if ([segue.identifier isEqualToString:@"toYiZhen"]) {
        QuestionDetailController *question = [segue destinationViewController];
        question.bqDesc = sender[@"content"];
        question.name = sender[@"name"];
        question.sex = sender[@"sex"];
        question.age = [NSString stringWithFormat:@"%@", sender[@"age"]];
        question.picUrls = sender[@"picUrl"];
        question.headIconUrl = sender[@"headImage"];
        question.doctorId = sender[@"doctorId"];
        question.askId = sender[@"askId"];
        if (_finish == YES) {
            question.ifDownload = 3;
        } else
            question.ifDownload = 2;
    }
}


@end
